package cn.cena.org.controller;

import cn.cena.basic.util.AjaxResult;
import cn.cena.basic.util.PageList;
import cn.cena.org.domian.Dictionarydetail;
import cn.cena.org.query.DictionarydetailQuery;
import cn.cena.org.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(tags = "字典明细接口",description = "字典明细接口详细描述")
public class DictionarydetailController {
    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /**
     * 查询一条
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value = "查询一条数据", notes = "传入一个id")
    public Dictionarydetail loadOne(@PathVariable("id") Long id) {
        return dictionarydetailService.loadOne(id);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有", notes = "不需要参数")
    public List<Dictionarydetail> loadAll() {
        return dictionarydetailService.loadAll();
    }

    /**
     * 高级查询
     *
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询", notes = "分页")
    public PageList<Dictionarydetail> loadByQuery(@RequestBody DictionarydetailQuery query) {
        System.out.println(dictionarydetailService.queryData(query));
        return dictionarydetailService.queryData(query);
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "根据id删除", notes = "传入一个id")
    public AjaxResult del(@PathVariable Long id) {
        try {
            dictionarydetailService.remove(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("删除失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除", notes = "传入多个id")
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            dictionarydetailService.patchRemove(ids);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "字典明细添加或修改", notes = "如果有id就是修改否则就是添加")
    public AjaxResult addAndUpdate(@RequestBody Dictionarydetail dictionarydetail) {
        try {
            if (dictionarydetail.getId() == null) {
                dictionarydetailService.add(dictionarydetail);
            } else {
                dictionarydetailService.update(dictionarydetail);
            }
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("操作失败");
        }
    }
}
